﻿@model Park
<form class="layui-form layui-form-pane" lay-filter="">
    <div class="layui-form-item">
        <label class="layui-form-label">园区名称</label>
        <div class="layui-input-inline">
            <input type="text" name="Code" autocomplete="off" class="layui-input" lay-verify="required" maxlength="20" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">所属单位</label>
        <div class="layui-input-inline">
            <select id="DeptID" name="DeptID" lay-verify="required">
                <option value="">请选择</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">所在省份</label>
        <div class="layui-input-inline">
            <select id="Province" name="Province" lay-filter="Province" lay-verify="required" lay-search>
                <option value="">请选择或搜索</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">所在城市</label>
        <div class="layui-input-inline">
            <select id="City" name="City" lay-filter="City" lay-verify="required" lay-search>
                <option value="">请选择或搜索</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">所在区县</label>
        <div class="layui-input-inline">
            <select id="County" name="County" lay-filter="County" lay-verify="required" lay-search>
                <option value="">请选择或搜索</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">园区地址</label>
        <div class="layui-input-inline">
            <input type="text" name="Address" autocomplete="off" class="layui-input" maxlength="100" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">园区面积</label>
        <div class="layui-input-inline">
            <input type="text" name="Area" autocomplete="off" class="layui-input" lay-verify="number" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">地址经度</label>
        <div class="layui-input-inline">
            <input type="text" name="Longitude" autocomplete="off" class="layui-input" lay-verify="number" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">地址维度</label>
        <div class="layui-input-inline">
            <input type="text" name="Latitude" autocomplete="off" class="layui-input" lay-verify="number" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">三维链接</label>
        <div class="layui-input-inline">
            <input type="text" name="Url3D" autocomplete="off" class="layui-input" maxlength="200" />
        </div>
    </div>
    <div class="layui-form-item" style="text-align:center;">
        <button class="layui-btn" lay-submit lay-filter="save">保存</button>
        <button class="layui-btn" id="cancel">取消</button>
    </div>
</form>
<script type="text/javascript">
    layui.use(['form', 'jquery'], function () {
        var form = layui.form;
        var $ = layui.jquery;
        var firstLoad = true;
        var deptList = @Html.Raw(ViewBag.DeptList);
        $(deptList).each(function (index, item) {
            $("#DeptID").append("<option value='" + item.ID + "'>" + item.DeptName + "</option>");
        });
        form.render("select");
        $.ajax({
            url: '/Local/Region/List',
            data: { code: 0 },
            success: function (res) {
                if (res.count > 0) {
                    $(res.data).each(function (index, item) {
                        $("#Province").append("<option value='" + item.value + "'>" + item.text + "</option>");
                    });
                    form.render("select");
                    if ("@Model.Province" != "") {
                        $("#Province").siblings("div.layui-form-select").find('dl dd[lay-value=' + @Model.Province + ']').click();
                    }
                    else {
                        firstLoad = false;
                    }
                }
            }
        });
        form.on('select(Province)', function (obj) {
            //移除城市下拉框所有选项
            $("#City").empty();
            $("#City").append("<option value=''>请选择或搜索</option>");
            $.ajax({
                url: '/Local/Region/List',
                data: { code: obj.value },
                success: function (res) {
                    if (res.count > 0) {
                        $(res.data).each(function (index, item) {
                            $("#City").append("<option value='" + item.value + "'>" + item.text + "</option>");
                        });
                        form.render("select");
                        if (firstLoad) {
                            if ("@Model.City" != "") {
                                $("#City").siblings("div.layui-form-select").find('dl dd[lay-value=' + @Model.City + ']').click();
                            }
                            else {
                                firstLoad = false;
                            }
                        }
                    }
                }
            });

        });
        form.on('select(City)', function (obj) {
            //移除城市下拉框所有选项
            $("#County").empty();
            $("#County").append("<option value=''>请选择或搜索</option>");
            $.ajax({
                url: '/Local/Region/List',
                data: { code: obj.value },
                success: function (res) {
                    if (res.count > 0) {
                        $(res.data).each(function (index, item) {
                            $("#County").append("<option value='" + item.value + "'>" + item.text + "</option>");
                        });
                        form.render("select");
                        if (firstLoad) {
                            if ("@Model.County" != "") {
                                $("#County").siblings("div.layui-form-select").find('dl dd[lay-value=' + @Model.County + ']').click();
                            }
                            firstLoad = false;
                        }
                    }
                }
            });

        });
        form.val('', {
            "Code": "@Model.Code",
            "DeptID": "@Model.DeptID",
            "Area": "@Model.Area",
            "Address": "@Model.Address",
            "Longitude": "@Model.Longitude",
            "Latitude": "@Model.Latitude",
            "Url3D": '@Model.Url3D'
        });

        form.on("submit(save)", function (formdata) {
            formdata.field.ID = "@Model.ID";
            formdata.field.ProvinceName = $("#Province").find("option:selected").text();
            formdata.field.CityName = $("#City").find("option:selected").text();
            formdata.field.CountyName = $("#County").find("option:selected").text();
            $.ajax({
                url: 'Edit',
                data: formdata.field,
                success: function (res) {
                    layer.alert(res.msg, function (index) {
                        layer.close(index);
                        if (res.count > 0) {
                            parent.layer.closeAll();
                        }
                    })
                }
            });
            return false;
        })
        $("#cancel").click(function () {
            parent.layer.closeAll();
        })
    })
</script>